$('#goodsList').bootstrapTable({
    classes: 'table table-bordered table-hover table-striped',
    url: 'getShopList',
    method: 'get',
    contentType:'application/json',
    dataType : 'json',        // 因为本示例中是跨域的调用,所以涉及到ajax都采用jsonp,
    uniqueId: 'goodsId',
    idField: 'goodsId',             // 每行的唯一标识字段
    cache:'false',
    toolbar: '#toolbar',       // 工具按钮容器
    buttonsToolbar: '#buttonsToolbar',
    showColumns: true,         // 是否显示所有的列
    showRefresh: true,         // 是否显示刷新按钮
    showSearchClearButton:true,
    //showToggle: true,        // 是否显示详细视图和列表视图的切换按钮(clickToSelect同时设置为true时点击会报错)

    pagination: true,                    // 是否显示分页
    sortOrder: "asc",                    // 排序方式
    sidePagination: "client",            // 分页方式：client客户端分页，server服务端分页
    pageNumber: 1,                       // 初始化加载第一页，默认第一页
    pageSize: 15,                        // 每页的记录行数     // 可供选择的每页的行数
    search: true,                      // 是否显示表格搜索，此搜索是客户端搜索

    //showExport: true,        // 是否显示导出按钮, 导出功能需要导出插件支持(tableexport.min.js)
    //exportDataType: "basic", // 导出数据类型, 'basic':当前页, 'all':所有数据, 'selected':选中的数据

    columns: [{
        checkbox:true,
    },{
        field: 'goodsId',
        title: 'ID',
        sortable: true
    }, {
        field: 'goodsName',
        title: '商品名称',
    }, {
        field: 'goodsImage',
        title: '商品图片',
        formatter:function (value) {
            return "<img style='width: 80px;height: 80px' src='"+value+"' alt=''>"
        }
    }, {
        field: 'goodsPrice',
        title: '兑换积分',
        sortable: true
    }, {
        field: 'goodsCount',
        title: '库存数量',
        sortable: true
    }, {
        field: 'goodsOperator',
        title: '操作人员'
    }, {
        field: 'createTime',
        title: '上架时间',
        sortable: true
    }],
    onLoadSuccess: function(data){
        $("[data-toggle='tooltip']").tooltip();
    }
});


var img_file;
var ischange = false;
$(".goods_img").change(function (e) {
    //console.log(e);
    var fileMsg = e.currentTarget.files;
    //console.log(fileMsg.length);
    if(fileMsg.length===0&&!ischange){
        $("#addGoods").attr("disabled","disabled");
        return;
    }else {
        ischange = true;
    }
    if(ischange){
        $("#addGoods").removeAttr("disabled");
    }
    if(fileMsg.length!==0){
        var objUrl = getObjectURL(this.files[0]);
        if(objUrl){
            $("#image").attr("src", objUrl);
            //console.log(this.files[0]);
            img_file = this.files[0];
        }
    }

});
//增加商品
$("#addGoods").click(function () {

    var goodsNumber = $("#goodsNumber");
    var goodsName = $("#goodsName");
    var goodsPrice = $("#goodsPrice");
    if(goodsName.val()===""){
        goodsName.focus();
        return;
    }else if(goodsPrice.val()===""){
        goodsPrice.focus();
        return;
    }else if(goodsNumber.val()===""){
        goodsNumber.focus();
        return;
    }else if (!ischange){
        return;
    }
    $("#addGoods").attr("disabled","disabled");
    $("#image").attr("src", "");
    var formData = new FormData();
    formData.append("image",img_file);
    formData.append("goodsName",goodsName.val());
    formData.append("goodsNumber",goodsNumber.val());
    formData.append("goodsPrice",goodsPrice.val());
    $.ajax({
        type: "post",
        url: "addGoods",
        contentType: false,
        processData: false,
        cache: false,
        data: formData,
        success:function (result) {
            //console.log(result);
            if (result===1){
                goodsName.val("");
                goodsPrice.val("");
                goodsNumber.val("");
                $('#goodsList').bootstrapTable('refresh');
                $("#addGoods").removeAttr("disabled");
                $("#foodsModal").modal('hide');
            }
        },
        error:function(data,type, err){
        }
    })
});

let goodsId = [];
//增加商品数量
$("#addGoodsCount").click(function () {
    var Name="";
    //console.log("增加商品数量");
    var goodsData=$("#goodsList").bootstrapTable('getSelections');
    if(goodsData.length===0){
        $("#confirm").attr("disabled","disabled");
        return;
    }else {
        $("#confirm").removeAttr("disabled");
    }
    for (let i = 0;i<goodsData.length;i++){
        Name = Name + goodsData[i].goodsName +" ";
        goodsId.push(goodsData[i].goodsId);
    }
    //console.log(goodsId);
    $("#name").text(Name);
});

$("#confirm").click(function () {
    var count = $("#goodsCount").val();
    if(count===""){
        $("#goodsCount").focus();
        return;
    }
    $.ajax({
        type: 'post',
        url: 'addGoodsCount',
        traditional: true,
        data:{
            goodsId: goodsId,
            count: count
        },
        success:function (result) {
            if(result===1){
                goodsId = [];
                $("#goodsList").bootstrapTable('refresh');
                $("#addGoodsCountModal").modal('hide');
            }
        }
    })
});